#!/bin/sh
########## SET THE CUSTOM VARIABLES ##########
#
# 设置自定义变量
# 当该程序运行结束，此处设置的变量自动取消
#
# USB_MOUNT_POINT: The mount point of the USB flash drive
# 设置自定义变量USB_MOUNT_POINT为U盘挂载点
USB_MOUNT_POINT=$(nvram get script_usbmount | sed 's/\/script_bootloader.*//')
#
# CHROOT_DIR: Folder with Debian Jessie
# 设置自定义变量CHROOT_DIR为debian环境系统目录
CHROOT_DIR=${USB_MOUNT_POINT}/debian
#
# EXT_DIR: Some folder outside of sandbox will be mounted to /mnt folder in Debian. Uncommented "EXT_DIR=" line if you need to mount a folder inside debian (remove #)
# 设置自定义变量EXT_DIR为debian环境外挂目录
EXT_DIR=${USB_MOUNT_POINT}/debian_mnt/
#
# CHROOT_SERVICES_LIST: List of services in debian
# 设置自定义变量CHROOT_SERVICES_LIST为debian服务启动列表
CHROOT_SERVICES_LIST=/opt/etc/chroot-services.list
#
########## END ##########
#
#
PATH=/opt/bin:/opt/sbin:/sbin:/bin:/usr/sbin:/usr/bin
if [ ! -e "$CHROOT_SERVICES_LIST" ]; then
echo "Please, define Debian services to start in
$CHROOT_SERVICES_LIST first!"
echo "One service per line. Hint: this is a script names from
Debian's /etc/init.d/"
exit 1
fi
MountedDirCount="$(mount | grep $CHROOT_DIR | wc -l)"
start() {
if [ $MountedDirCount -gt 0 ]; then
echo "Chroot'ed services seems to be already started,
exiting..."
exit 1
fi
echo "Starting chroot'ed Debian services..."
for dir in dev proc sys; do
mount -o bind /$dir $CHROOT_DIR/$dir
done
[ -z "$EXT_DIR" ] || mount -o bind $EXT_DIR $CHROOT_DIR/mnt
for item in $(cat $CHROOT_SERVICES_LIST); do
chroot $CHROOT_DIR /etc/init.d/$item start
done
}
stop() {
if [ $MountedDirCount -eq 0 ]; then
echo "Chroot'ed services seems to be already stopped,
exiting..."
exit 1
fi
echo "Stopping chroot'ed Debian services..."
for item in $(cat $CHROOT_SERVICES_LIST); do
chroot $CHROOT_DIR /etc/init.d/$item stop
sleep 2
done
umount /opt/debian/dev/pts
mount | grep $CHROOT_DIR | awk '{print $3}' | xargs umount -l
}
restart() {
if [ $MountedDirCount -eq 0 ]; then
echo "Chroot'ed services seems to be already stopped"
start
else
echo "Stopping chroot'ed Debian services..."
for item in $(cat $CHROOT_SERVICES_LIST); do
chroot $CHROOT_DIR /etc/init.d/$item stop
sleep 2
done
mount | grep $CHROOT_DIR | awk '{print $3}' | xargs umount -l
echo "Restarting chroot'ed Debian services..."
for dir in dev proc sys; do
mount -o bind /$dir $CHROOT_DIR/$dir
done
[ -z "$EXT_DIR" ] || mount -o bind $EXT_DIR $CHROOT_DIR/mnt
for item in $(cat $CHROOT_SERVICES_LIST); do
chroot $CHROOT_DIR /etc/init.d/$item start
done
fi
}
enter() {
[ -z "$EXT_DIR" ] || mount -o bind $EXT_DIR $CHROOT_DIR/mnt
mount -o bind /dev/ /opt/debian/dev/
mount -o bind /dev/pts /opt/debian/dev/pts
mount -o bind /proc/ /opt/debian/proc/
mount -o bind /sys/ /opt/debian/sys/
chroot /opt/debian /bin/bash
}
status() {
if [ $MountedDirCount -gt 0 ]; then
echo "Chroot'ed services running..."
else
echo "Chroot'ed services not running!"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
enter)
enter
;;
status) status
;;
*)
echo "Usage: (start|stop|restart|enter|status)"
exit 1
;;
esac
echo Done.
exit 0